package com.rongke.web.api;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.rongke.commons.JsonResp;
import com.rongke.model.BankcardInfo;
import com.rongke.model.User;
import com.rongke.service.BankcardInfoService;
import com.rongke.service.UserService;
import com.rongke.web.thirdapi.BankCard;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @BankcardInfoController
 * @用户认证Controller
 * @version : Ver 1.0
 */
@RestController
@RequestMapping(value="/api/bankcardInfo")
@Transactional
@CrossOrigin
public class BankcardInfoController {
    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private BankcardInfoService bankcardInfoService;
    @Autowired
    private UserService userService;
    /**
     * @添加用户认证
     *
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/addBankCard", method = RequestMethod.GET)
    public JsonResp addBankcardInfo( String realName,String idCard,String bankNum){
        log.debug("添加银行卡信息");
        User loginUser = userService.findLoginUser();
        EntityWrapper<BankcardInfo> bankcardInfoEntityWrapper = new EntityWrapper<>();
        bankcardInfoEntityWrapper.eq("user_id",loginUser.getId());
        bankcardInfoEntityWrapper.eq("bank_num",bankNum);
        BankcardInfo bankcardInfo1 = bankcardInfoService.selectOne(bankcardInfoEntityWrapper);
        if (bankcardInfo1!=null){
            return JsonResp.fa("该银行卡已被当前用户绑定");
        }
        BankCard bankCard = new BankCard();
        BankcardInfo bankcardInfo = new BankcardInfo();
        bankcardInfo.setBankNum(bankNum);
        bankcardInfo.setIdCard(idCard);
        bankcardInfo.setRealName(realName);
        boolean b = bankCard.checkBankCard(bankcardInfo, loginUser.getPhone());
        System.out.println(b);
        if (b){
            bankcardInfo.setUserId(loginUser.getId());
            bankcardInfo.setStatus(0);
            bankCard.setBankcardInfo(bankcardInfo);

            if (bankcardInfo.getBankName()==null){
                return JsonResp.fa("该银行卡不支持");
            }
            boolean insert = bankcardInfoService.insert(bankcardInfo);
            if (insert){
                return JsonResp.ok("银行卡添加成功");
            }
        }
//        bankcardInfoService.insert(bankcardInfo);
        return JsonResp.fa("银行卡添加失败");
    }

    /**
     * @修改用户认证
     * @param bankcardInfo
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/update", method = RequestMethod.POST)
    public JsonResp updateBankcardInfo(@RequestBody BankcardInfo bankcardInfo){
        log.debug("修改用户认证");
        bankcardInfoService.updateById(bankcardInfo);
        return JsonResp.ok(bankcardInfo);
    }
    /**
     * @根据id查找用户认证
     * @param id
     * @return 返回值JsonResp
     */
    @RequestMapping(value="/selectOne", method = RequestMethod.GET)
    public JsonResp selectBankcardInfo(Long id){
        log.debug("查找用户认证");
        BankcardInfo bankcardInfo = bankcardInfoService.selectById(id);
        return JsonResp.ok(bankcardInfo);
    }

    /**
     * @备注:查询显示当前用户的银行卡
     * @return
     */
    @RequestMapping(value = "/selectBankInfo",method = RequestMethod.GET)
    public JsonResp getBankInfo(){
        User loginUser = userService.findLoginUser();
        EntityWrapper<BankcardInfo> bankcardInfoEntityWrapper = new EntityWrapper<>();
        bankcardInfoEntityWrapper.eq("user_id",loginUser.getId());
        bankcardInfoEntityWrapper.eq("status",0);
        List<BankcardInfo> bankcardInfos = bankcardInfoService.selectList(bankcardInfoEntityWrapper);
        if (bankcardInfos.size()>0){
            return JsonResp.ok(bankcardInfos);
        }
        return JsonResp.fa("暂无银行卡信息");
    }

}
